import pytest,time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_produt_mysql_locator import ProductMysqlLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.common_data import Commondata


class  UserProductMysql(Basepage):

    def enter_my_resource_page(self):
        doc = '进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(ProductManagementLocator.my_resource_tab, doc)
                self.click_element(ProductManagementLocator.my_resource_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(ProductManagementLocator.check_order_button, doc)
        self.click_element(ProductManagementLocator.check_order_button, doc)
        try:
            self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        except:
            pass
        self.wait_elevisble(ProductManagementLocator.page_total, doc)
        self.wait_elevisble(ProductManagementLocator.check_order_button, doc)
        self.click_element(ProductManagementLocator.check_order_button, doc)
        time.sleep(2)



    def create_mysql_instance_subscription(self):
         doc='购买Mysql主实例(subscription)'
         self.enter_my_resource_page()
         self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
         self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
         time.sleep(2)
         self.get_prod_name_to_click('RDS for MySQL')
         time.sleep(2)
         self.switch_to_new_window()
         try:
            self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
         except:
             pass
         intance_name = CommonTool.generate_string()
         self.wait_elevisble(ProductManagementLocator.instance_name_input, doc)
         time.sleep(2)
         self.send_keys(ProductManagementLocator.instance_name_input, intance_name, doc)
         self.wait_elevisble(ProductMysqlLocator.ip_white_list,doc)
         self.send_keys(ProductMysqlLocator.ip_white_list,'192.168.1.0',doc)
         self.click_element(ProductManagementLocator.term_of_service, doc)
         self.button_is_enabled(ProductManagementLocator.buy_now_button)
         self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
         self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
         time.sleep(2)
         self.wait_elevisble(ProductManagementLocator.pay_button, doc)
         time.sleep(2)
         self.click_element(ProductManagementLocator.pay_button, doc)
         self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
         self.driver.close()
         all_handles = self.driver.window_handles
         self.driver.switch_to.window(all_handles[0])
         return intance_name


    def create_mysql_instance_pay_as_you_go(self):
         doc='购买Mysql主实例(pay_as_you_go)'
         self.enter_my_resource_page()
         self.wait_elevisble(ProductManagementLocator.cerate_new_tab, doc)
         self.move_mouse_to_element(ProductManagementLocator.cerate_new_tab)
         time.sleep(2)
         self.get_prod_name_to_click('RDS for MySQL')
         time.sleep(2)
         self.switch_to_new_window()
         try:
            self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
         except:
             pass
         self.click_element(ProductMysqlLocator.pay_as_you_go_button,doc)
         intance_name = CommonTool.generate_string()
         self.wait_elevisble(ProductManagementLocator.instance_name_input, doc)
         time.sleep(2)
         self.send_keys(ProductManagementLocator.instance_name_input, intance_name, doc)
         self.wait_elevisble(ProductMysqlLocator.ip_white_list,doc)
         self.send_keys(ProductMysqlLocator.ip_white_list,'192.168.1.0',doc)
         self.click_element(ProductManagementLocator.term_of_service, doc)
         self.button_is_enabled(ProductManagementLocator.buy_now_button)
         self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
         self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
         time.sleep(2)
         self.wait_elevisble(ProductManagementLocator.pay_button, doc)
         time.sleep(2)
         self.click_element(ProductManagementLocator.pay_button, doc)
         self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
         self.driver.close()
         all_handles = self.driver.window_handles
         self.driver.switch_to.window(all_handles[0])
         return intance_name


    def release_subscripition_ecs(self,cmdopt,instance_id):
        doc = '释放subscripition_ecs'
        self.driver.get(Commondata.home_page[cmdopt]['user'])
        self.enter_my_resource_page()
        self.query_intance_id(instance_id)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.action_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.action_button, doc)
        time.sleep(2)
        self.get_action_to_click('Release')
        self.switch_to_new_window()
        self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.release_button,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.release_button,doc)
        self.wait_elevisble(ProductManagementLocator.buy_now_button,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.buy_now_button,doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful,doc)

    def renew_subscripition_ecs(self, cmdopt, instance_id):
        doc = '续费ecs'
        self.driver.get(Commondata.home_page[cmdopt]['user'])
        self.enter_my_resource_page()
        self.query_intance_id(instance_id)
        self.wait_elevisble(ProductManagementLocator.order_view_detail_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.order_view_detail_button, doc)
        self.switch_to_new_window()
        try:
            self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        except:
            pass
        self.wait_elevisble(ProductManagementLocator.term_of_service, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.term_of_service, doc)
        self.button_is_enabled(ProductManagementLocator.buy_now_button)
        try:
            self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        except:
            pass
        self.wait_elevisble(ProductManagementLocator.pay_button, doc)
        self.click_element(ProductManagementLocator.pay_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])

    def query_intance_id(self,instance_id):
        doc='查询intance_id'
        self.wait_elevisble(ProductManagementLocator.instance_id_input,doc)
        self.keyboard_event(ProductManagementLocator.instance_id_input,instance_id)
        self.send_keys(ProductManagementLocator.instance_id_input,instance_id,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.instance_id_search,doc)
        self.wait_elevisble(ProductManagementLocator.order_view_detail_button,doc)

    def get_action_to_click(self,action_name):
        doc='获取操作名称然后点击'
        self.wait_elevisble(ProductManagementLocator.my_resource_action_list,doc)
        action_list_eles =self.get_elements(ProductManagementLocator.my_resource_action_list,doc)
        for action in action_list_eles:
            if action.text== action_name:
                action.click()


    def charge_transfer_sub_psgy(self,cmdopt,instance_id):
        doc='计费互转'
        self.driver.get(Commondata.home_page[cmdopt]['user'])
        self.enter_my_resource_page()
        self.query_intance_id(instance_id)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.billing_method_change,doc)
        self.click_element(ProductManagementLocator.billing_method_change,doc)
        self.switch_to_new_window()
        try:
            self.wait_elevisble(ProductManagementLocator.loading_wait,doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait,doc)
        except:
            pass
        self.wait_elevisble(ProductManagementLocator.term_of_service,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.term_of_service,doc)
        time.sleep(2)
        self.button_is_enabled(ProductManagementLocator.billing_method_change_buy_now)
        try:
            self.wait_elevisble(ProductManagementLocator.loading_wait,doc)
            self.wait_eleinvisble(ProductManagementLocator.loading_wait,doc)
        except:
            pass
        self.wait_elevisble(ProductManagementLocator.pay_button,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.pay_button,doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful,doc)
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])





    def get_prod_name_to_click(self,prod_name):
        doc='获取产品名称然后点击'
        prod_list=self.get_elements(ProductManagementLocator.prod_list,doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)


    def button_is_enabled(self,locate):
        doc='判断按钮是否可以点击'
        button=self.get_element(locate,doc)
        for i in range(5):
            time.sleep(2)
            if  button.is_enabled():
                button.click()
                break


    def wait_status_to_running(self,intance_name):
        doc='等待状态变成running'
        self.refresh_page()
        self.wait_elevisble(ProductManagementLocator.menu_list, doc)
        self.click_element(ProductManagementLocator.menu_list, doc)
        self.get_console_product_to_click('RDS for MySQL')
        time.sleep(4)
        self.wait_elevisble(ProductManagementLocator.iframe, doc)
        self.driver.switch_to.frame(self.get_element(ProductManagementLocator.iframe, doc))
        self.wait_elevisble(ProductMysqlLocator.status, doc)
        self.query_instance(intance_name)
        time.sleep(2)
        self.wait_elevisble(ProductMysqlLocator.instance_id_ele,doc)
        intance_id=self.get_element(ProductMysqlLocator.instance_id_ele,doc).text
        print(intance_id)
        while True:
            time.sleep(5)
            status=self.get_element(ProductMysqlLocator.status, doc).text
            if status == 'Running':
                break
            else:
                self.click_element(ProductManagementLocator.refresh_button,doc)
        return  intance_id


    def query_instance(self,intance_name):
        doc='查询intance_name'
        self.wait_elevisble(ProductMysqlLocator.console_instance_name_input,doc)
        self.keyboard_event(ProductMysqlLocator.console_instance_name_input,doc)
        self.send_keys(ProductMysqlLocator.console_instance_name_input,intance_name,doc)
        self.click_element(ProductMysqlLocator.console_search_button,doc)
        self.wait_elevisble(ProductMysqlLocator.status,doc)




    def get_console_product_to_click(self,prouduct_name):
        doc = '查询产品名称然后点击'
        self.wait_elevisble(ProductManagementLocator.product_name_input,doc)
        self.send_keys(ProductManagementLocator.product_name_input,prouduct_name,doc)
        self.wait_elevisble(ProductManagementLocator.search_prod_ele,doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.search_prod_ele,doc)
        time.sleep(2)


